package com.dwb.service;

import com.dwb.config.BizException;
import com.dwb.entity.PNews;
import com.dwb.entity.PNewsColumn;
import com.dwb.mappers.PNewsColumnMapper;
import com.dwb.mappers.PNewsMapper;
import com.dwb.result.ResultCode;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.List;
import java.util.Map;

import static com.dwb.common.check.Check.*;

/**
 * @Author wx
 * @create 2020/6/28 10:59
 */
@Service
public class PNewsColumnService {

    @Autowired
    private PNewsColumnMapper sNewsColumnMapper;

    @Autowired
    private PNewsMapper sNewsMapper;

    public PNewsColumn getInfo(Map<String, String> parameters) {
        CHECK_GET_INFO(parameters);
        return sNewsColumnMapper.selectByPrimaryKey(parameters.get("id"));

    }

    public PageInfo<PNewsColumn> getList(Map<String, String> parameters) {
        CHECK_GET_LIST(parameters);
        PageHelper.startPage(Integer.parseInt(parameters.get("pageNum") == null ? "" : "1"),
                Integer.parseInt(parameters.get("pageSize")==null ? "" : "10"));
        List<PNewsColumn> SNewsColumns = sNewsColumnMapper.selectAll();
        return new PageInfo<>(SNewsColumns);
    }


    public void insert(PNewsColumn sNewsColumn) {
        CHECK_SNEWS_COLUM_INSET_OR_UPDATE(sNewsColumn);

        sNewsColumnMapper.insert(sNewsColumn);

    }



    public void update(PNewsColumn sNewsColumn) {

        CHECK_SNEWS_COLUM_INSET_OR_UPDATE(sNewsColumn);
        sNewsColumnMapper.updateByPrimaryKeySelective(sNewsColumn);
    }


    public void delete(Map<String, String> parameters) {
        CHECK_GOODS_CATEGORY_DELETE(parameters);
        Example example = new Example(PNews.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("id",parameters.get("id"));
        List<PNews> sGoodsCategories = sNewsMapper.selectByExample(example);
        if (sGoodsCategories.size()>0){
            throw new BizException(ResultCode.CATEGORY_NOT_DELETE);
        }
        sNewsColumnMapper.deleteByPrimaryKey(parameters.get("id"));
    }
}
